import xlrd
import requests
import json

#excelPath = "C:\\work\\code\\etag\\5 eStation\\51 Server\\Inuit\\package\\errorcode.xls"
#codePath = "C:\\work\\code\\etag\\5 eStation\\51 Server\\Inuit\Inuit.Framework\\Inuit.Framework.Common\\MsgCode.cs"
#url = "http://localhost:5018/sys/Language/sync2"

excelPath = "ErrorCode.xls"
codePath = "..\\Inuit.Framework\\Inuit.Framework.Common\\MsgCode.cs"
url = "http://192.168.4.25:9001/inuit/sys/Language/sync2"


def read_excel(file_path):
    table_data = []
    # 打开 Excel 文件读取数据
    workbook = xlrd.open_workbook(file_path)
    # 获取所有的 Sheet 名称
    all_sheets = workbook.sheet_names()
    for sheet_name in all_sheets:
       
        # 获取指定 Sheet 工作表内容
        table = workbook.sheet_by_name(sheet_name)
        # 遍历每一行
        for row_index in range(table.nrows):
            if(row_index == 0):
                continue
            rowValue = table.row_values(rowx=row_index)
            data = {
                "classComment": rowValue[0],
                "className": rowValue[1],
                "contentEnum": rowValue[2],
                "contentValue": str(int(rowValue[3])),
                "contentCn": rowValue[4],
                "contentEn": rowValue[5],
            }
            table_data.append(data)
    return table_data


def writeCode(data):
    header = """
    namespace Inuit.Framework.Common;

    /// <summary>
    /// 状态码枚举
    /// </summary>
    public class MsgCode
    {
    """

    bodyHeader = """
        /// <summary>
        /// {classComment}
        /// </summary>
        public enum {className}
        {{
    """
    
    bodyContent = """
            /// <summary>
            /// {contentCn}
            /// </summary>
            {contentEnum} = {contentValue},
    """
        
    bodyFoot = """
        }
    """
    
    foot = """
    }
    """

    code = header

    key = ""
    for i in range(len(data)):
        if key == data[i]["className"]:
            code += bodyContent.format(**data[i])
        else:
            if key != "":
                code += bodyFoot
            code += bodyHeader.format(**data[i])
            key = data[i]["className"]
            code += bodyContent.format(**data[i])
    code += bodyFoot
    code += foot
    
    f1 = open(codePath, 'w', encoding = 'utf-8')
    f1.write(code)
    f1.close()

def importData(data):
    res = []
    for item in data:
        res.append( {
                "group": "SYS",
                "code": "en",
                "key": item['contentValue'],
                "value": item['contentEn']
        })

        res.append( {
                "group": "SYS",
                "code": "zh-CN",
                "key": item['contentValue'],
                "value": item['contentCn']
        })
    headers = {'Content-Type': 'application/json'}
    response = requests.post(url, data=json.dumps(res), headers=headers)
    print(response.text)


excel_data = read_excel(excelPath)

print("生成语言文件")
writeCode(excel_data)

print("导入数据库")
importData(excel_data)



price_str = input("完成")